package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.entity.SubjectAnswer;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
 * 判断题
 * */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //插入判断题目
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectInfoBO.getOptionLists().get(0).getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectTypeEnum getHandlerType() {
        return SubjectTypeEnum.JUDGE;
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));

        List<SubjectJudge> subjectJudgeList = subjectJudgeService.queryByCondition(subjectJudge);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        List<SubjectAnswer> subjectAnswerList = new ArrayList<>();

        subjectJudgeList.forEach(judge -> {
            SubjectAnswer subjectAnswer = new SubjectAnswer();
            subjectAnswer.setIsCorrect(judge.getIsCorrect());
            subjectAnswerList.add(subjectAnswer);
        });

        subjectOptionBO.setOptionLists(subjectAnswerList);
        return subjectOptionBO;
    }
}
